package com.example.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.example.config.ApplicationContextHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.DataSourceFactory;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.jta.JtaTransactionManager;

import javax.annotation.Resource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.sql.XAConnection;
import javax.sql.XADataSource;
import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

@Repository("bookDao")
public class BookDao extends BaseDao{

//    @Resource()
//    private JdbcTemplate jdbcTemplate1;
//
//    private JdbcTemplate jdbcTemplate2;

//    @Autowired
//    private JtaTransactionManager transactionManager;
    @Autowired
    private ApplicationContextHelper applicationContextHelper;
//    public BookDao(JndiObjectFactoryBean jndiObjectFactoryBean) {
//        super(jndiObjectFactoryBean);
//    }

    //    @Autowired
//    private JdbcTemplate jdbcTemplate;
   public void testJNDI() throws Exception{
        Context ctx = new InitialContext();
       XADataSource ds = (XADataSource) ctx.lookup("java:comp/env/jdbc_db1");
        XAConnection conn = ds.getXAConnection();
        System.out.println(conn.getConnection());
    }

    public void testJNDI2() throws Exception{
        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc_db1");
        Connection conn = ds.getConnection();
        System.out.println(conn);
    }
    @Transactional
    public void test() throws Exception{

//        try {
            String sql = "insert into test(id, name) values(6, '444')";
            jdbcTemplate1.execute(sql);  // 1库
            jdbcTemplate2.execute(sql);  // 2库
//        } catch (Exception e) {
////            e.printStackTrace();
//throw new RuntimeException("");
//        }
    }

//    @Transactional
    public void test4() throws Exception{
       // GRANT XA_RECOVER_ADMIN ON *.* TO root@'%' ;

//        Object db1 = applicationContextHelper.getApplicationContext().getBean("db1");
//        XADataSource s=  (XADataSource)db1;
//        XAConnection conn = s.getXAConnection();
//        System.out.println(conn.getConnection());
        UserTransaction userTransaction = transactionManager.getUserTransaction();
        userTransaction.begin();
        try {
            String sql = "insert into test(id, name) values(6, '444')";
            jdbcTemplate1.execute(sql);
            jdbcTemplate2.execute(sql);
            userTransaction.commit();
        } catch (Exception e) {
            userTransaction.rollback();
            e.printStackTrace();

        }
    }


    public void test3(){
        try {
            String sql = "select * from test";
            List<Map<String, Object>> maps = jdbcTemplate1.queryForList(sql);
            List<Map<String, Object>> maps2 = jdbcTemplate2.queryForList(sql);

            System.out.println(maps.size()+"   "+maps2.size());
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
//        jdbcTemplate.queryForList("select * from book");
    }

    public void test2(){
        try {
//            Object db1 = applicationContextHelper.getApplicationContext().getBean("db1");
//            DruidDataSource dataSource = (DruidDataSource)db1;
////            DruidDataSourceFactory
//            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
////    System.out.println(dataSource);
////            jndiTemplate.getContext()
//            List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from test");
//            System.out.println(list.size());
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
//        jdbcTemplate.queryForList("select * from book");
    }

    public static void main(String[] args) {
    }

//    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
//        this.jdbcTemplate = jdbcTemplate;
//    }
}
